﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skimpt3.Plugins.Interface;
using System.Xml;
using Skimpt3;

namespace KalleLoad
{
    public class KalleLoadPlugin : IUploaderInterface
    {

        public string Name
        {
            get { return "KalleLoad"; }
        }

        public string FormURL
        {
            get { return "http://www.kalleload.net/?skimpt"; }
        }

        public string FileBoxName
        {
            get { return "selector"; }
        }

        private string strFileUrl = null;
        public string FileUrl
        {
            get { return strFileUrl; }
        }
 
        private string strThumbUrl = null;
        public string ThumbUrl
        {
            get { return strThumbUrl; }
        }

        private Image imgImage = null;
        public Image Image
        {
            get { return imgImage; }
            set { imgImage = value; }
        }

        private string strFileName = null;
        public string FileName
        {
            get { return strFileName; }
            set { strFileName = value; }
        }

        public void Upload()
        {
            Skimpt3.PostFile post = new Skimpt3.PostFile(FormURL, FileName);

            post.AddFormElement("MAX_UPLOAD_SIZE", "104857600");
            post.AddFormElement("progress", "Upload");
            post.FileBoxName = FileBoxName;
            post.ContentMIMEType = "image/jpeg";

            try
            {
                post.Send(Image);
            }
            catch (Exception ex)
            {
                throw ex;
            }

			/*
			 * Hopefully, post.Response will be returned as MIME:text/xml
			 * In which case we can pass it in to a new System.XmlDocument and pass out the bits we want
			 * System.XmlDocument: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
			 *
			 * Else it's going to be an HTML scraping job
			 * Might also be able to pass that in to a new System.XmlDocument
			 * Other wise it's substrings all the way :(
			 */

            try
            {
                XmlDocument xDoc = new XmlDocument();
                Console.WriteLine(post.Response);
                xDoc.LoadXml(new System.IO.StringReader(post.Response).ReadToEnd());

                XmlNodeList nodeList = xDoc.GetElementsByTagName("property");

                strFileUrl = nodeList[0].InnerXml;
                strThumbUrl = nodeList[1].InnerXml;

                foreach (XmlNode node in nodeList)
                {
                    Console.WriteLine(node.Name + " content = " + node.InnerText + " ++ " + node.InnerXml + " ++ " + node.Value);
                    Console.WriteLine(node.Name + " outer = " + node.OuterXml);
                    foreach (XmlAttribute attr in node.Attributes)
                    {
                        Console.WriteLine(node.Name + "@" + attr.Name + " = " + attr.InnerText);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

			/*
			 * There could be more outputs here, such as:
			 * ->BB Code
			 * ->HTML Code
			 * ->Thumbnail
			 *
			 * These would have to have their own fields:
			 *
			 * private string strFieldName = "";
			 * public string FieldName
			 * {
			 * 		get { return strFieldName; }
			 * }
			 */
        }
    }
}
